package com.jar.exercise.多线程.AQS.LockSupport.线程交替;

import com.jar.exercise.多线程.TheadTools;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *
 *
 *  </div>
 *
 * @author zhangshun
 * @date 2021/3/10 12:49
 */
public class LockSupperTest {

    static Thread thread1 = null;
    static Thread thread2 = null;

    public static void main(String[] args) {



        AtomicInteger atomicInteger = new AtomicInteger(0);
        final Object[] objects1 = {1, 2, 3, 4,5,6,7,8,9,10};


        thread1 = TheadTools.getThread(() -> {
            while (atomicInteger.get() < objects1.length) {
	            System.out.println(Thread.currentThread().getName() + "->" + objects1[atomicInteger.get()]);
	            atomicInteger.incrementAndGet();
                LockSupport.unpark(thread2);
                LockSupport.park();
            }
            LockSupport.unpark(thread2);

        }, "线程1");




        thread2 = TheadTools.getThread(() -> {
            LockSupport.park();
            while (atomicInteger.get() < objects1.length) {
	            System.out.println(Thread.currentThread().getName() + "->" + objects1[atomicInteger.get()]);
	            atomicInteger.incrementAndGet();
                LockSupport.unpark(thread1);
                LockSupport.park();
            }
            LockSupport.unpark(thread1);

        }, "线程2");

        thread1.start();
        thread2.start();

    }




}
